
<template>
	<view>
		<view v-if="dataBuffer" >
			<view class="item"
				v-for="(res, index) in siteList" :key="res.id" 
			>	<u-icon name="close-circle-fill" :size="40" 
			color="#ff5500"
			 @click="remove(index,res.id)" 
			 :index="res.id"></u-icon>
				<view class="top"  @click="goBack(index)">
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag">
						<text class="red" v-if="res.status == 1">默认</text>
					</view>
				</view>
				<view class="bottom" >
					{{res.location+res.useraddress}}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id"  ></u-icon>
				</view>
			</view>
		</view>
	
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			siteList: [],//初始化地址数据
			dataBuffer:true,
		};
	},
	onShow() {
		let userInfo=this.$tool.isLogin()
		if(userInfo ==false)return;	
			//获取用户全部收货地址
	this.getAddress(userInfo.uid)
	},
	
	methods: {
		//切换收货地址
		goBack(index){
			uni.navigateTo({
				url:"../order/order?addres="+JSON.stringify(this.siteList[index])
			})
		},
		//删除地址数据
		remove(index,id){
			uni.showModal({
			title:"温馨提示",
			content:"删除后不可恢复",
			success:res=>{
				if(res.confirm){
					//同步页面
					this.siteList.splice(index,1)
					//同步数据库
					this.$http({
						url:"addressremove",
						method:"post",
						data:{id}
					}).then(res=>{
						if(this.siteList.length==0){
							this.dataBuffer=false
						}
					})
				}
			}
			})
			
		},
		getAddress(uid){
			//获取用户全部收货地址
			// console.log(this.userInfo.uid);
			this.$http({
				url:"addresslist",
				data:{uid}
			}).then(res=>{
				console.log(res);
				if(res.list==null){
					this.dataBuffer=false
				}else{
					this.siteList=res.list
				}
			})
		},
		// 跳转到新增地址页面
		toAddSite(){
			uni.navigateTo({
				url:'./addSite'
			})
		}
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/address.scss';
</style>
